package com.example.demo.controller.front;

import com.example.demo.dao.Activity;
import com.example.demo.dao.Goods;
import com.example.demo.dao.ImagePath;
import com.example.demo.dao.Shopcart;
import com.example.demo.service.GoodsService;
import com.example.demo.service.ShopCartService;
import com.example.demo.util.Msg;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/shopcar")
public class ShopcartController {
    @Autowired
    private ShopCartService ShopCartService;
@Autowired
private GoodsService goodsService;
    //商品加入购物车
    @PostMapping(value = "/add")
    @ResponseBody
    public Msg addshopcar(Goods goods, HttpSession session, HttpServletRequest request){
        Integer userid =(Integer)session.getAttribute("id");

        Date time = new Date();
        java.sql.Date date = new java.sql.Date(time.getTime());
        Integer id= goods.getGoodsid();
        Integer num = goods.getNum();
        Shopcart shopcart = new Shopcart(userid,id,date,num);
        int add= ShopCartService.addshopcar(shopcart);
        if(add!=0){
            return Msg.success("添加成功");
        }else {
            return Msg.fail("添加失败");
        }

    }

//    //跳入购物车界面
//    @RequestMapping(value = "/goto")
//    public String gotoshopcat(){
//        return "shopcar";
//    }

    //查找购物车
    @GetMapping(value = "/search")

    public String  searchShopCar(HttpSession session,Model model){
        Integer userid = (Integer) session.getAttribute("id");
        List<Shopcart> shopcars=ShopCartService.searchShopcar(userid);
        List<Goods> goods =new ArrayList<>();
        for(Shopcart shopcart:shopcars){
            Goods goods1 =goodsService.findgoods(shopcart.getGoodsid());
            goods1.setNum(shopcart.getGoodsnum());
            goods.add(goods1);
        }
        model.addAttribute("goodsImage",goods);
        List<Activity> activities=new ArrayList<>();
        return "shopcar";
        }

        //增加商品数量
//    @PostMapping(value = "/addnum")
//    public updatenum
}
